home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 21 / Mac Magazin and MacEasy Magazine CD - Issue 21.iso / Wissenschaft & Technik / yorick12vr1-nofpu folder / include / ascii.i next >
Text File  |  1996-02-21  |  4KB  |  108 lines

  1. /*
  2.  * ascii.i --
  3.  *
  4.  *    Reading ascii data from a file.
  5.  *
  6.  * $Id: ascii.i,v 1.2 1996/02/20 12:45:40 eric Exp $
  7.  *
  8.  * Copyright (c) 1996, Eric THIEBAUT (thiebaut@obs.univ-lyon1.fr, Centre de
  9.  * Recherche Astrophysique de Lyon, 9 avenue Charles  Andre,  F-69561 Saint
  10.  * Genis Laval Cedex).
  11.  *
  12.  * This program is free software; you can redistribute it and/or  modify it
  13.  * under the terms of the GNU General Public License  as  published  by the
  14.  * Free Software Foundation; either version 2 of the License,  or  (at your
  15.  * option) any later version.
  16.  *
  17.  * This program is distributed in the hope  that  it  will  be  useful, but
  18.  * WITHOUT  ANY   WARRANTY;   without   even   the   implied   warranty  of
  19.  * MERCHANTABILITY or  FITNESS  FOR  A  PARTICULAR  PURPOSE.   See  the GNU
  20.  * General Public License for more details (to receive a  copy  of  the GNU
  21.  * General Public License, write to the Free Software Foundation, Inc., 675
  22.  * Mass Ave, Cambridge, MA 02139, USA).
  23.  *
  24.  * ------------------------------------------------------------------------
  25.  * History:
  26.  *    02/20/96: release 1.1
  27.  *    02/20/96 by Eric THIEBAUT: improve performances of asciiRead()
  28.  *        execution time almost divided by 2, "string.i" no more needed.
  29.  *      02/21/96 D Munro improved performance by a factor of 250 or so.
  30.  */
  31.  
  32. func asciiRead(file)
  33. /* DOCUMENT data= asciiRead(name)
  34.             data= asciiRead(file)
  35.     read ascii numeric data in columns from file NAME, or the already
  36.     open file FILE.
  37.     The result is a NCOLUMNS-by-NLINES array of doubles.
  38.  
  39.     Data are read as double values arranged in columns separated
  40.     by any number of spaces or tabs.  Comments starting with a "#"
  41.     or any other character which is not part of a number are ignored
  42.     up to the end-of-line.  Blank lines are ignored.
  43.     The first non-blank/commented line gives the number of values per
  44.     column, for subsequent lines.  Subsequent lines must have the
  45.     same number of columns -- blanks in columns are not permitted,
  46.     use 0.0 instead.  However, minimal error checking is performed,
  47.     and if the data is not really in columns, asciiRead can silently
  48.     fail to interpret your file as you would scanning it by eye.
  49.  
  50.     The read operation will be much faster if the number of commented
  51.     lines is relatively small.  Blank lines cost nothing, while a line
  52.     containing just a "#" is expensive.
  53.  
  54.    SEE ALSO: read
  55. */
  56. {
  57.   /* open the file if it's not already open */
  58.   if (structof(file)==string) file= open(file);
  59.  
  60.   /* read lines one at a time until the "model" line which
  61.    * determines the number of columns is discovered
  62.    * assume the number of columns is less than 128 */
  63.   x= array(0.0, 128);
  64.   ncols= 0;
  65.   while ((line= rdline(file))) {
  66.     ncols= sread(line, x);
  67.     if (ncols) break;          /* got a line with numbers */
  68.   }
  69.   if (!ncols) return [];
  70.  
  71.   nrows= 1;
  72.   list= _lst([x(1:ncols)]);
  73.   x= array(0.0, ncols, 10000/ncols + 1);
  74.   for(;;) {
  75.     /* try to grab at least 10000 numbers from the file
  76.      * blank lines will be skipped, but any comments will
  77.      * interrupt the read */
  78.     n= read(file, x);
  79.     if (!n) {
  80.       /* if didn't get any, drop back to reading comments one
  81.        * line at a time until we get some more numbers */
  82.       while ((line= rdline(file))) {
  83.     n= sread(line, x);
  84.     if (n) break;
  85.       }
  86.       if (!line) break;    /* rdline detected end-of-file, n==0 too */
  87.     }
  88.     if (n%ncols) error, "data is not in columns";
  89.     n/= ncols;
  90.  
  91.     /* grow the list the fast way, adding new values to its head
  92.      * (adding to the tail would make growth an n^2 proposition,
  93.      *  as would using the grow function) */
  94.     list= _cat(x(,1:n), list);
  95.     nrows+= n;
  96.   }
  97.  
  98.   /* pop chunks off list and reassemble result */
  99.   x= array(0.0, ncols, nrows);
  100.   for (i=nrows ; list ; list=_cdr(list)) {
  101.     n= numberof(_car(list))/ncols;
  102.     x(,i-n+1:i)= _car(list);
  103.     i-= n;
  104.   }
  105.  
  106.   return x;
  107. }
  108.